﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
	[Serializable()]
	public class NhaCungCapBO : Csla.BusinessBase<NhaCungCapBO>
	{
		#region Business Properties
		
private int? _NhaCungCapID = null;
public int? NhaCungCapID
{
get { return this._NhaCungCapID; }
set { this._NhaCungCapID = value; PropertyHasChanged("NhaCungCapID"); }
}

private string _TenNhaCungCap = null;
public string TenNhaCungCap
{
get { return this._TenNhaCungCap; }
set { this._TenNhaCungCap = value; PropertyHasChanged("TenNhaCungCap"); }
}

private string _DienThoai = null;
public string DienThoai
{
get { return this._DienThoai; }
set { this._DienThoai = value; PropertyHasChanged("DienThoai"); }
}

private string _DiDong = null;
public string DiDong
{
get { return this._DiDong; }
set { this._DiDong = value; PropertyHasChanged("DiDong"); }
}

private string _Fax = null;
public string Fax
{
get { return this._Fax; }
set { this._Fax = value; PropertyHasChanged("Fax"); }
}

private string _Email = null;
public string Email
{
get { return this._Email; }
set { this._Email = value; PropertyHasChanged("Email"); }
}

private string _Diachi = null;
public string Diachi
{
get { return this._Diachi; }
set { this._Diachi = value; PropertyHasChanged("Diachi"); }
}

private string _NguoiLienHe = null;
public string NguoiLienHe
{
get { return this._NguoiLienHe; }
set { this._NguoiLienHe = value; PropertyHasChanged("NguoiLienHe"); }
}

private string _MaSoThue = null;
public string MaSoThue
{
get { return this._MaSoThue; }
set { this._MaSoThue = value; PropertyHasChanged("MaSoThue"); }
}

private string _SoTaiKhoan = null;
public string SoTaiKhoan
{
get { return this._SoTaiKhoan; }
set { this._SoTaiKhoan = value; PropertyHasChanged("SoTaiKhoan"); }
}

private string _TaiNganHang = null;
public string TaiNganHang
{
get { return this._TaiNganHang; }
set { this._TaiNganHang = value; PropertyHasChanged("TaiNganHang"); }
}

private string _GhiChu = null;
public string GhiChu
{
get { return this._GhiChu; }
set { this._GhiChu = value; PropertyHasChanged("GhiChu"); }
}

private int? _USRC = Session.LoginID;
public int? USRC
{
get { return this._USRC; }
set { this._USRC = value; PropertyHasChanged("USRC"); }
}

private DateTime? _DATC = DateTime.MaxValue;
public DateTime? DATC
{
get { return this._DATC; }
set { this._DATC = value; PropertyHasChanged("DATC"); }
}

private int? _USRM = null;
public int? USRM
{
get { return this._USRM; }
set { this._USRM = value; PropertyHasChanged("USRM"); }
}

private DateTime? _DATM = null;
public DateTime? DATM
{
get { return this._DATM; }
set { this._DATM = value; PropertyHasChanged("DATM"); }
}

private bool? _DELE = false;
public bool? DELE
{
get { return this._DELE; }
set { this._DELE = value; PropertyHasChanged("DELE"); }
}

		private int? _STT0 = 0;
		public int? STT0
		{
			get { return _STT0; }
			set { _STT0 = value; PropertyHasChanged("STT0"); }
		}
		
		protected override object GetIdValue()
		{
			return _NhaCungCapID.Value;
		}
		#endregion Business Properties
		//==========================================================================
		
		#region Factory Methods
		public void SetIsNew()
		{
			MarkNew();
		}

		public void SetIsOld()
		{
			MarkOld();
		}
		
		public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }
		
		private static int newNhaCungCapID =  -1;
private NhaCungCapBO()
{
_NhaCungCapID = newNhaCungCapID--;
}

		public NhaCungCapBO(SqlDataReader dataReader)
		{
			MarkAsChild();
			Fetch(dataReader);
			MarkOld();
		}

		public NhaCungCapBO(SqlDataReader dataReader, bool isRoot)
		{
			if (!isRoot)
                MarkAsChild();
			Fetch(dataReader);
			MarkOld();
		}

		public static NhaCungCapBO NewRootNhaCungCapBO()
		{
			return DataPortal.Create<NhaCungCapBO>(new RootCriteria());
		}

		internal static NhaCungCapBO NewChildNhaCungCapBO()
		{
			return DataPortal.Create<NhaCungCapBO>(new ChildCriteria());
		}

		public static NhaCungCapBO GetRootNhaCungCapBO(int _NhaCungCapID)
		{
			return DataPortal.Fetch<NhaCungCapBO>(new RootCriteria(_NhaCungCapID));
		}

		public static NhaCungCapBO GetRootNhaCungCapBOByWhere(string where)
		{
			return DataPortal.Fetch<NhaCungCapBO>(where);
		}

		internal static NhaCungCapBO GetChildNhaCungCapBO(SqlDataReader dataReader)
		{
			return new NhaCungCapBO(dataReader);
		}

		public static void DeleteNhaCungCapBO(int _NhaCungCapID)
		{
			DataPortal.Delete(new RootCriteria(_NhaCungCapID));
		}

		public static void DeleteNhaCungCapBO(int _NhaCungCapID, bool directDelete)
		{
			DataPortal.Delete(new RootCriteria(_NhaCungCapID, directDelete));
		}
		#endregion Factory Methods
		//==========================================================================
		
		#region Criteria
		[Serializable()]
		public class RootCriteria
		{
			public int NhaCungCapID;
			public bool directDelete = false;
			public RootCriteria(int _NhaCungCapID)
			{
				this.NhaCungCapID = _NhaCungCapID;
				this.directDelete = false;
			}
			
			public RootCriteria(int _NhaCungCapID, bool directDelete)
            {
                this.NhaCungCapID = _NhaCungCapID;
                this.directDelete = directDelete;
            }
            
			public RootCriteria() {}
		}

		[Serializable()]
		public class ChildCriteria
		{
			public int NhaCungCapID;
			public bool directDelete = false;
			public ChildCriteria(int _NhaCungCapID)
			{
				this.NhaCungCapID = _NhaCungCapID;
				this.directDelete = false;
			}
			
			public ChildCriteria(int _NhaCungCapID, bool directDelete)
            {
                this.NhaCungCapID = _NhaCungCapID;
                this.directDelete = directDelete;
            }
            
			public ChildCriteria() {}
		}
		#endregion Criteria
		//==========================================================================

		#region Create
		[RunLocal]
		private void DataPortal_Create(RootCriteria criteria)
		{
		}

		private void DataPortal_Create(ChildCriteria criteria)
		{
			MarkAsChild();
			ValidationRules.CheckRules();
		}

		#endregion Create
		//==========================================================================

		#region Fetch
		[Transactional(TransactionalTypes.TransactionScope)]
		protected void DataPortal_Fetch(RootCriteria criteria)
		{
			this.Execute_Fetch(criteria);
		}

		protected void DataPortal_Fetch(string where)
		{
			this.Execute_Fetch(where);
		}

		protected void Execute_Fetch(RootCriteria criteria)
		{
			this.This_Fetch(criteria);
		}
		
		protected void Execute_Fetch(string where)
		{
			this.This_Fetch(where);
		}

		private void This_Fetch(RootCriteria criteria)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("NhaCungCapBO_GetBO");
			db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[NhaCungCapID] = " + criteria.NhaCungCapID);
			using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
					Fetch(dataReader);                    
			}
		}

		private void This_Fetch(string where)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("NhaCungCapBO_GetBO");
			db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
			using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
					Fetch(dataReader);                    
			}
		}

		private void Fetch(SqlDataReader dataReader)
		{
			if (!(dataReader["NhaCungCapID"] is DBNull))
this._NhaCungCapID = (int)dataReader["NhaCungCapID"];
if (!(dataReader["TenNhaCungCap"] is DBNull))
this._TenNhaCungCap = (string)dataReader["TenNhaCungCap"];
if (!(dataReader["DienThoai"] is DBNull))
this._DienThoai = (string)dataReader["DienThoai"];
if (!(dataReader["DiDong"] is DBNull))
this._DiDong = (string)dataReader["DiDong"];
if (!(dataReader["Fax"] is DBNull))
this._Fax = (string)dataReader["Fax"];
if (!(dataReader["Email"] is DBNull))
this._Email = (string)dataReader["Email"];
if (!(dataReader["Diachi"] is DBNull))
this._Diachi = (string)dataReader["Diachi"];
if (!(dataReader["NguoiLienHe"] is DBNull))
this._NguoiLienHe = (string)dataReader["NguoiLienHe"];
if (!(dataReader["MaSoThue"] is DBNull))
this._MaSoThue = (string)dataReader["MaSoThue"];
if (!(dataReader["SoTaiKhoan"] is DBNull))
this._SoTaiKhoan = (string)dataReader["SoTaiKhoan"];
if (!(dataReader["TaiNganHang"] is DBNull))
this._TaiNganHang = (string)dataReader["TaiNganHang"];
if (!(dataReader["GhiChu"] is DBNull))
this._GhiChu = (string)dataReader["GhiChu"];
if (!(dataReader["USRC"] is DBNull))
this._USRC = (int)dataReader["USRC"];
if (!(dataReader["DATC"] is DBNull))
this._DATC = (DateTime)dataReader["DATC"];
if (!(dataReader["USRM"] is DBNull))
this._USRM = (int)dataReader["USRM"];
if (!(dataReader["DATM"] is DBNull))
this._DATM = (DateTime)dataReader["DATM"];
if (!(dataReader["DELE"] is DBNull))
this._DELE = (bool)dataReader["DELE"];

		}
		#endregion Fetch
		//==========================================================================

		#region Insert
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Insert()
		{
			this.Execute_Insert();
		}

		protected void Execute_Insert()
		{
			this.This_Insert();
		}

		private void This_Insert()
		{
			this._USRC = Session.LoginID;
this._DATC = DateTime.MaxValue;
this._USRM = null;
this._DATM = null;
this._DELE = false;

			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("NhaCungCapBO_InsertBO");
			db.AddInParameter(cmd, "@TenNhaCungCap", SqlDbType.NVarChar, this._TenNhaCungCap);
db.AddInParameter(cmd, "@DienThoai", SqlDbType.NVarChar, this._DienThoai);
db.AddInParameter(cmd, "@DiDong", SqlDbType.NVarChar, this._DiDong);
db.AddInParameter(cmd, "@Fax", SqlDbType.NVarChar, this._Fax);
db.AddInParameter(cmd, "@Email", SqlDbType.NVarChar, this._Email);
db.AddInParameter(cmd, "@Diachi", SqlDbType.NVarChar, this._Diachi);
db.AddInParameter(cmd, "@NguoiLienHe", SqlDbType.NVarChar, this._NguoiLienHe);
db.AddInParameter(cmd, "@MaSoThue", SqlDbType.NVarChar, this._MaSoThue);
db.AddInParameter(cmd, "@SoTaiKhoan", SqlDbType.NVarChar, this._SoTaiKhoan);
db.AddInParameter(cmd, "@TaiNganHang", SqlDbType.NVarChar, this._TaiNganHang);
db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
db.AddInParameter(cmd, "@USRC", SqlDbType.Int, this._USRC);
db.AddOutParameter(cmd, "@NewNhaCungCapID", SqlDbType.Int, 10);
db.AddOutParameter(cmd, "@NewDATC", SqlDbType.DateTime, 4);
			db.ExecuteNonQuery(cmd);
			this._NhaCungCapID = (int)cmd.Parameters["@NewNhaCungCapID"].Value;
this._DATC = (DateTime)cmd.Parameters["@NewDATC"].Value;
		}
		#endregion Insert
		//==========================================================================

		#region Update
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Update()
		{
			this.Execute_Update();
		}

		protected void Execute_Update()
		{
			this.This_Update();
		}

		private void This_Update()
		{
			this._USRM = Session.LoginID;
this._DATM = DateTime.Now;

			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("NhaCungCapBO_UpdateBO");
			db.AddInParameter(cmd, "@NhaCungCapID", SqlDbType.Int, this._NhaCungCapID);
db.AddInParameter(cmd, "@TenNhaCungCap", SqlDbType.NVarChar, this._TenNhaCungCap);
db.AddInParameter(cmd, "@DienThoai", SqlDbType.NVarChar, this._DienThoai);
db.AddInParameter(cmd, "@DiDong", SqlDbType.NVarChar, this._DiDong);
db.AddInParameter(cmd, "@Fax", SqlDbType.NVarChar, this._Fax);
db.AddInParameter(cmd, "@Email", SqlDbType.NVarChar, this._Email);
db.AddInParameter(cmd, "@Diachi", SqlDbType.NVarChar, this._Diachi);
db.AddInParameter(cmd, "@NguoiLienHe", SqlDbType.NVarChar, this._NguoiLienHe);
db.AddInParameter(cmd, "@MaSoThue", SqlDbType.NVarChar, this._MaSoThue);
db.AddInParameter(cmd, "@SoTaiKhoan", SqlDbType.NVarChar, this._SoTaiKhoan);
db.AddInParameter(cmd, "@TaiNganHang", SqlDbType.NVarChar, this._TaiNganHang);
db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
db.AddInParameter(cmd, "@USRM", SqlDbType.Int, this._USRM);
db.AddInParameter(cmd, "@DELE", SqlDbType.Bit, this._DELE);

db.AddOutParameter(cmd, "@NewDATM", SqlDbType.DateTime, 4);
			db.ExecuteNonQuery(cmd);
			
this._DATM = (DateTime)cmd.Parameters["@NewDATM"].Value;
		}
		#endregion Update
		//==========================================================================

		#region Delete
		[Transactional(TransactionalTypes.TransactionScope)]
		protected void DataPortal_DeleteSelf()
		{
			DataPortal_Delete(new RootCriteria(_NhaCungCapID.Value));
		}

		private void DataPortal_Delete(RootCriteria criteria)
		{
			this.Execute_Delete(criteria);
		}

		protected void Execute_Delete(RootCriteria criteria)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("NhaCungCapBO_DeleteBO");
			db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
			db.AddInParameter(cmd, "@NhaCungCapID", SqlDbType.Int, criteria.NhaCungCapID);
			db.ExecuteNonQuery(cmd);
		}
		#endregion Delete
		//==========================================================================

		#region Internal method
		internal void Insert()
		{
			if (!this.IsDirty) return;
			DataPortal_Insert();
			MarkOld();
		}

		internal void Update()
		{
			if (!this.IsDirty) return;
			DataPortal_Update();
			MarkOld();
		}

		internal void DeleteSelf()
		{
			if (!this.IsDirty) return;
			if (this.IsNew) return;
			DataPortal_Delete(new RootCriteria(_NhaCungCapID.Value));
			MarkNew();
		}
		#endregion Internal method
		//==========================================================================
	}
}
